package com.wxf.exceltoword.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.wxf.exceltoword.service.DataFilterService;
import com.wxf.exceltoword.util.CsvUtils;
import com.wxf.exceltoword.util.DocUtil;
import com.wxf.exceltoword.util.ExcelUtils;


@Service
public class DataFilterServiceImpl implements DataFilterService {
    private final Logger log = LoggerFactory.getLogger(getClass());
	  /**
     * excel数据导入数据库
     * @param normalFile
     */
    @Override
    public void importExcel(File normalFile,File docTemFile) throws Exception {

        List<List<Object>> list = null;
        // 1、判断文件类型
        String fileName = normalFile.getPath();
        String fileType = fileName.substring(fileName.lastIndexOf("."));

        if(".csv".equalsIgnoreCase(fileType)){
            list = CsvUtils.getBankListByCSV(normalFile);
            log.info(" ** 解析CSV文件完成，共{}行。",list.size());
        } else {
            InputStream input = new FileInputStream(normalFile);
            list = ExcelUtils.getBankListByExcel(input,normalFile.getPath());
            log.info(" ** 解析Excel文件完成，共{}行。",list.size());
        }
//        List<Object[]> params = new ArrayList<>();
        Map<String,String> contentMap = new HashMap<String,String> ();
        File destFile = new File("D:/exceltoword/");
			
		
		if (!destFile.exists()){
			destFile.mkdirs();
		}
        for(List<Object> rows : list){
			System.out.println("woshishi:"+rows.get(0));
		contentMap.clear();
		contentMap.put("name", rows.get(0).toString()==null?"":rows.get(0).toString());
		contentMap.put("sex", rows.get(1).toString()==null?"":rows.get(1).toString());
		contentMap.put("city", rows.get(2).toString()==null?"":rows.get(2).toString());
		contentMap.put("company", rows.get(3).toString()==null?"":rows.get(3).toString());
		contentMap.put("position", rows.get(4).toString()==null?"":rows.get(4).toString());
		contentMap.put("tel", rows.get(5).toString()==null?"":rows.get(5).toString());
		contentMap.put("experience", rows.get(6).toString()==null?"":rows.get(6).toString());
		DocUtil.build(docTemFile, contentMap, "D:/exceltoword/"+rows.get(5).toString()+"-"+rows.get(0).toString()+".doc");

            /*params.add(new Object[]{
                    UUID.randomUUID().toString().replaceAll("-",""),
                    DateUtils.toDate(rows.get(0).toString(),"yyyy-MM-dd"),
                    String.valueOf(rows.get(1)), String.valueOf(rows.get(2)), String.valueOf(rows.get(3)),
                    String.valueOf(rows.get(4)), String.valueOf(rows.get(5)), String.valueOf(rows.get(6)),
                    String.valueOf(rows.get(7)), String.valueOf(rows.get(8)), String.valueOf(rows.get(9)),
                    String.valueOf(rows.get(10)), String.valueOf(rows.get(11)), String.valueOf(rows.get(12)),
                    String.valueOf(rows.get(13)), String.valueOf(rows.get(14)), String.valueOf(rows.get(15)),
                    String.valueOf(rows.get(16))
            });*/
        }
        long startTime = System.currentTimeMillis();

        log.info(" ** 批量插入完成,耗时：{}秒",(System.currentTimeMillis()-startTime)/1000);
    }
//    @Resource
//    private DemoMapper demoMapper;
//
//    @Override
//    public int deleteByPrimaryKey(String id) {
//        return demoMapper.deleteByPrimaryKey(id);
//    }
//
//    @Override
//    public int insert(Demo record) {
//        return demoMapper.insert(record);
//    }
//
//    @Override
//    public int insertSelective(Demo record) {
//        return demoMapper.insertSelective(record);
//    }
//
//    @Override
//    public Demo selectByPrimaryKey(String id) {
//        return demoMapper.selectByPrimaryKey(id);
//    }
//
//    @Override
//    public int updateByPrimaryKeySelective(Demo record) {
//        return demoMapper.updateByPrimaryKeySelective(record);
//    }
//
//    @Override
//    public int updateByPrimaryKey(Demo record) {
//        return demoMapper.updateByPrimaryKey(record);
//    }
}
